<?php

/**
* alltosun.com 评论控制器 index.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-8-11 下午12:46:09 $
* $Id$
*/

class Action
{
    private $per_page = 20;
    /**
     * 显示评论列表
     */
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::Get('status', 1);
        $is_advenced   = Request::getParam('is_advenced', 0);
        $search_filter = Request::getParam('search_filter', array());
        $goods_id      = Request::getParam('goods_id', 0);

        // 拼装搜索条件
        $is_empty = 0;
        $filter = array();
        if (isset($search_filter['content']) && $search_filter['content']) {
            $filter['`title` LIKE '] = '%'.$search_filter['content'].'%';
        }
        if (isset($search_filter['user_name']) && $search_filter['user_name']) {
            $filter['user_id'] = user_helper::get_user_ids_by_name($search_filter['user_name']);
            if (!$filter['user_id']) {
                $is_empty = 1;
            }
        }
        if (isset($search_filter['goods_title']) && $search_filter['goods_title']) {
            //通过快照查询goods_snapshot_id
            $goods_filter['`title` LIKE '] = '%'.$search_filter['goods_title'].'%';
            $goods_snapshot_ids = _model('goods_snapshot')->getFields('id', $goods_filter);
            if ($goods_snapshot_ids) {
                $filter['res_id'] = $goods_snapshot_ids;
            } else {
                $is_empty = 1;
            }
        }
        if (isset($search_filter['res_id']) && $search_filter['res_id']) {
            $filter['res_id'] = $search_filter['res_id'];
        }
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'].' 23:59:59';
        }
        if (isset($goods_id) && $goods_id) {
            $filter['res_id'] = goods_helper::get_comment_res_ids($goods_id);
        }
        $filter['status'] = $status;
        $filter['parent_id'] = 0;

        $comment_list = array();
        if (!$is_empty) {
            $comment_count = _model('comment')->getTotal($filter);
            if ($comment_count) {
                $pager = new Pager($this->per_page);
                if ($pager->generate($comment_count)) {
                    Response::assign('pager', $pager);
                }

                $comment_list = _model('comment')->getList($filter, ' ORDER BY `id` DESC '.$pager->getLimit($page));
            }
        }

        Response::assign('comment_list', $comment_list);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);
        Response::assign('is_advenced', $is_advenced);
        Response::display('admin/comment_list.html');
    }

    /**
     * 禁用
     * @return array
     */
    public function delete()
    {
        $id = Request::getParam('id');

        if (!$id) {
            return '请选择要删除的内容';
        }

        $ids = explode(',', trim($id, ','));
        foreach ($ids as $k=>$v) {
            if (!$v) {
                continue;
            }

            $comment_info = _uri('comment', (int)$v);
            if (!$comment_info) {
                continue;
            }

            if (1 == $comment_info['status']) {
                _model('comment')->update($v, array('status'=>0));
            }
        }

        return 'ok';
    }

    /**
     * 更新状态
     * @return array
     */
    public function change_status()
    {
        $id     = Request::getParam('id', 0);
        $status = Request::getParam('status', 1);
        if (!$id) {
            return '请选择要更改的内容';
        }

        $comment_info = _uri('comment', $id);
        if ($status == $comment_info['status']) {
            return '已经执行过此操作，不用重复提交';
        }

        if (1 == $status) {
            _model('comment')->update($id, array('status'=>1));
        }

        return 'ok';
    }

    public function get_comment_children()
    {
        $id = Request::Post('id', 0);

        $info = _uri('comment', array('id'=>$id, 'status'=>1));

        if (!$info) {
            return '该评论不存在或已删除';
        }

        $list = array();
        $list = _model('comment')->getList(array('parent_id'=>$id));

        return $list;
    }

    public function add_comment()
    {
        $value = Request::Post('value', '');
        $comment_id = Request::Post('comment_id', 0);
        $id = Request::Post('id', 0);

        if (!$value) {
            return '请填写要添加的内容';
        }

        $info = _uri('comment', array('id'=>$comment_id, 'status'=>1));

        if (!$info) {
            return '操作失败，要回复的评论不存在或已删除';
        }
        $uesr_id = user_helper::get_user_id();
        if ($id) {
            _model('comment')->update($id, array('title'=>$value, 'parent_id'=>$comment_id, 'user_id'=>$uesr_id));
        } else {
            $id = _model('comment')->create(array('title'=>$value, 'parent_id'=>$comment_id, 'user_id'=>$uesr_id));
        }

        return _uri('comment', $id);
    }
}
?>